<?php
namespace Home\Controller;

class IndexController extends PublicController
{
    public function index()
    {
        $comment=M('comment');
        $like=M('like');
        $article=M('article');
        $article_pic=M('article_pic');
        $list =$article->table("blog_article a,blog_user_info ui")
            ->field("a.aid,a.uid,a.title,a.click,a.ds,ui.nickname")
            ->where("a.uid=ui.uid")
            ->select();

        for ($i=0;$i<count($list);$i++)
        {
            $count=$comment->field("COUNT(cmtid) counts")
                ->where('aid='.$list[$i]['aid'])
                ->select();
            $likes=$like->field("COUNT(aid) likes")
                ->where('aid='.$list[$i]['aid'])
                ->select();
            $iname=$article_pic->field('iname')
                ->where('aid='.$list[$i]['aid'])
                ->select();
            $list[$i]['comments']=$count[0]['counts'];
            $list[$i]['likes']=$likes[0]['likes'];
            if ($iname) {
                $list[$i]['iname'] = $iname[0]['iname'];
            }else{
                $list[$i]['iname'] = '1.png';
            }
        }
        $this->assign('list',$list);
//        p($list);die;
        $this->display();
    }
    
    public function goadd()
    {
        $this->pageload();
        $this->redirect(U('home/article/add'));
    }
    
    public function get_catelist()
    {
        $cid = I('git.cid',0,'intval');
        if ($cid == 0)
        {

        }
        $comment=M('comment');
        $like=M('like');
        $article=M('article');
        $list =$article->table("blog_article a,blog_user_info ui")
            ->field("a.aid,a.uid,a.title,a.click,a.ds,ui.nickname")
            ->where("a.uid=ui.uid")
            ->select();

        for ($i=0;$i<count($list);$i++)
        {
            $count=$comment->field("COUNT(cmtid) counts")
                ->where('aid='.$list[$i]['aid'])
                ->select();
            $likes=$like->field("COUNT(aid) likes")
                ->where('aid='.$list[$i]['aid'])
                ->select();
            $list[$i]['comments']=$count[0]['counts'];
            $list[$i]['likes']=$likes[0]['likes'];
        }
        $this->assign('list',$list);
    }
}